/*
 * Copyright 2013 Agustina Martinez-Garcia

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package uk.ljmu.metadata.schemasutilities;

import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;

import uk.ljmu.metadata.utilities.MetadataUtilities;

public class DoapUtilities {

	private static final Logger log = Logger.getLogger(DoapUtilities.class);
	
	public static Document contructDoapDocument(List<String> prefixList) {
		
		Element root = DocumentHelper.createElement( "rdf:RDF" );
		root.addNamespace("rdf", MetadataUtilities.RELS_MAP.get("rdf"));
		root.addNamespace("rdfs", MetadataUtilities.RELS_MAP.get("rdfs"));
		root.addNamespace("doap", MetadataUtilities.MDSchemasUrl.get("doap"));

		if (prefixList != null && prefixList.size() > 0)
		{
			for (String pref : prefixList)
			{
				String ns = MetadataUtilities.MDSchemasUrl.get(pref);
				if (ns == null)
					continue;
				root.addNamespace(pref, ns);
			}
		}
		/*<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:doap="http://usefulinc.com/ns/doap#">
		 <doap:Project>
		  <doap:name>Example project</doap:name>
		  <doap:homepage rdf:resource="http://exampleproject.com" />  
		  <doap:programming-language>javascript</programming-language>
		  <doap:license rdf:resource="http://usefulinc.com/doap/licenses/gpl"/> 
		 </doap:Project>
		</rdf:RDF>*/

		//Create document
		root.addElement("doap:Project");
		Document doc = DocumentHelper.createDocument(root);

		return doc;
	}
	
	/**
	 * Add a property to an existing RDF description record
	 * 
	 * @param propertyName string with the property name
	 * @param propertyValue string with the value of the property
	 * @param xmlContents String containing the actual record in which to add the new property
	 * 
	 * @return Document with the properties added
	 */
	public static Document addDoapProperty(String propertyName, String propertyValue, String xmlContents)
	{
		//Create document
		Document doc = null;
		try 
		{
			doc = DocumentHelper.parseText(xmlContents);
		} catch (DocumentException e) {
			log.fatal(e.getMessage());
		}
		if (doc != null)
		{
			Element root = doc.getRootElement();
			Element desc = root.element("Project");
			Namespace ns = new Namespace("doap", MetadataUtilities.MDSchemasUrl.get("doap"));
			QName qname;
			//Already Qualified
			if (!propertyName.contains(":"))
				qname = new QName(propertyName, ns);
			else qname = new QName(propertyName);
			Element property = DocumentHelper.createElement(qname);
			property.setText(propertyValue);
			desc.add(property);

			return doc;
		} 
		else
		{
			log.info("Error while creating XML Document");
			return null;
		}
	}
}
